home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / prog / graphics / ch_hull.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  1.9 KB  |  128 lines

  1. #include <LEDA/plane.h>
  2. #include <LEDA/window.h>
  3.  
  4. window W;
  5.  
  6. list<point> C_HULL(list<point> L)
  7.   if (L.length() < 3) return L;
  8.  
  9.   list<point> CH;
  10.   list_item last;
  11.   point p;
  12.  
  13.   L.sort();  // sort points lexicographically
  14.  
  15.  
  16.   // initialize convex hull with first two points
  17.  
  18.   p = L.pop();
  19.   CH.append(p);
  20.   while (L.head() == p) L.pop();
  21.   last = CH.append(L.pop());
  22.  
  23.  
  24.   // scan remaining points
  25.  
  26.   forall(p,L)
  27.   {
  28.     if (p == CH[last]) continue;  // multiple point
  29.  
  30.     // compute upper tangent (p,up)
  31.  
  32.     list_item up = last;
  33.     list_item it = CH.cyclic_succ(up);
  34.  
  35.     while (left_turn(CH[it],CH[up],p))
  36.     { up = it;
  37.       it = CH.cyclic_succ(up);
  38.      }
  39.  
  40.  
  41.     // compute lower tangent (p,low)
  42.  
  43.     list_item low = last;
  44.     it = CH.cyclic_pred(low);
  45.  
  46.     while (right_turn(CH[it],CH[low],p))
  47.     { low = it;
  48.       it = CH.cyclic_pred(low);
  49.      }
  50.  
  51.  
  52.     // remove all points between up and low
  53.  
  54.     if (up != low)
  55.     { it = CH.cyclic_succ(low);
  56.  
  57.       while (it != up)
  58.       { CH.del(it);
  59.         it = CH.cyclic_succ(low);
  60.        }
  61.      }
  62.  
  63.     // insert new point
  64.  
  65.     last = CH.insert(p,low);
  66.  
  67.    }
  68.  
  69.   return CH;
  70. }
  71.      
  72.  
  73.  
  74. main()
  75. {
  76.   //window W;
  77.   W.init(-100,100,-100);
  78.   W.set_node_width(5);
  79.  
  80.   int N = 500;
  81.  
  82.   panel P("convex hull");
  83.  
  84.   P.int_item("# points",N,1,2000);
  85.   int b1 = P.button("mouse");
  86.   int b2 = P.button("random");
  87.   int b3 = P.button("quit");
  88.  
  89.   for(;;)
  90.   { 
  91.     list<point> L;
  92.     point p,q;
  93.  
  94.     int but = P.open();
  95.  
  96.     W.clear();
  97.  
  98.     if (but == b1)
  99.       while (W >> p)
  100.       { W.draw_point(p,blue);
  101.         L.append(p);
  102.        }
  103.  
  104.     if (but == b2)
  105.       for(int i = 0; i<N; i++) 
  106.       { point p(random(-90,90),random(-90,90));
  107.         W.draw_point(p,blue);
  108.         L.append(p);
  109.        }
  110.  
  111.     if (but == b3) break;
  112.   
  113.     list<point> C = C_HULL(L);
  114.   
  115.     p = C.tail();
  116.   
  117.     forall(q,C) 
  118.     { W.draw_segment(p,q,violet);
  119.       W.draw_filled_node(q,violet);
  120.       p = q;
  121.      }
  122.   }
  123.    
  124.  return 0;
  125. }
  126.  
  127.